package com.liy.teaching.common;


import com.liy.teaching.entity.ProductEntity;
import com.liy.teaching.service.ProductService;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 *
 * 初始化, 将所有的 商品ID 放入布隆过滤器中
 *
 */
@Component
public class RedisDataInit {

    //前缀
    private static final String KEY_PREFIX = "product:";


    @Autowired
    private ProductService productService;


    @Autowired
    private RBloomFilter<String> redisBloomFilter;


    @PostConstruct
    public void inti(){

        List<ProductEntity> allProductList = productService.list();

        for(ProductEntity product: allProductList){
            String id = product.getId();
            String redisKey = buildKey(id);

            redisBloomFilter.add(redisKey);   // 布隆过滤器中加入redisKey
        }

    }


    //  格式化一下
    private static String buildKey(String id) {
        return KEY_PREFIX + id;  // product:商品ID
    }
}
